<?php
    include '../JHeader.php';
    require '../Database/Database.php';

    function GetLeague($league_id)
    {
      $db = new Database();
      $db->Connect();
      $leagues = $db->Select('Leagues', 'ID_League = \'' . $league_id . '\'');
      $db->Disconnect();

      return $leagues[0];
    }

    if(isset($_GET['id']))
    {
      $id = $_GET['id'];
      $db = new Database();
      $db->Connect();
      $series = $db->Select('Series', 'ID_Series =  ' . $id);
      $db->Disconnect();
      if($series)
      {
          $series = $series[0];
          $league = GetLeague($series->League_ID);
?>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/default.css" />
        <script type="text/javascript" language="JavaScript" src="../js_helpfuncs/helpfuncs.js"></script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Bearbeite "<?php echo $series->Name ?>"</title>
    </head>
    <body>
       <h1><?php echo $series->Name ?> bearbeiten</h1>
        <form name="edit_form" action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
            <div class="changeable_values">
                <table>
                    <tr>
                        <td>Name: <input type="text" name="series_name" size="25" /></td>
                        <td>aktuell: <?php echo $series->Name ?></td>
                    </tr>
                    <tr>
                       <td>Kürzel: <input type="text" name="series_shortcut" size="10" /></td>
                       <td>aktuell: <?php echo $series->Shortcut ?></td>
                    </tr>
                    <tr>
                       <td>Maximale Fahrer: <input type="text" name="series_maxdriv" size="10" /></td>
                       <td>aktuell: <?php echo $series->Maximum_drivers ?></td>
                    </tr>
                    <tr>
                       <td>Maximale Fahrer pro Team: <input type="text" name="series_maxdrivteam" size="10" /></td>
                       <td>aktuell: <?php echo $series->Maximum_drivers_per_team ?></td>
                    </tr>
                    <tr>
                       <?php 
                       $steam_string = '';
                       if($series->Steam == 1)
                       {
                           $steam_string = 'erforderlich';
                           echo '<td>Steam: <input type="checkbox" name="series_steam" value="val_steam" checked /></td>';
                       }
                       else
                       {
                           $steam_string = 'nicht erforderlich';
                           echo '<td>Steam: <input type="checkbox" name="series_steam" value="val_steam" /></td>';
                       }
                       ?>
                        <td>aktuell: <?php echo $steam_string ?></td>
                    </tr>
                </table>
            </div>
            <div class="constant_values">
                <table>
                    <tr>
                        <td>Season <?php echo $series->Season ?></td>
                    </tr>
                    <tr>
                        <td>Simulation: <?php echo $league->Game ?></td>
                    </tr>
                    <tr>
                        <td>Liga: <?php echo $league->Name ?></td>
                    </tr>
                </table>
            </div>
            <div>
                <input type="hidden" name="id" value="<?php echo $id ?>" />
                <input type="hidden" name="old_name" value="<?php echo $series->Name ?>" />
                <input type="hidden" name="old_shortcut" value="<?php echo $series->Shortcut ?>" />
                <input type="hidden" name="old_maxdriv" value="<?php echo $series->Maximum_drivers ?>" />
                <input type="hidden" name="old_maxdrivteam" value="<?php echo $series->Maximum_drivers_per_team ?>" />
                <input type="hidden" name="old_steam" value="<?php echo $steam_string ?>" />
                <input type="submit" name="edit" value="Ändern" onclick="return checkAndConfirm( 'edit_form', 'Wirklich ändern?')" />
            </div>
        </form>
    </body>
</html>
<?php
      }
      else
      {
          echo 'Ung&uuml;ltige Serien ID.';
      }
    }
    else if(isset($_POST['edit']))
    {
        $id              = $_POST['id'];
        $name            = $_POST['series_name'];
        $shortcut        = $_POST['series_shortcut'];
        $steam_val       = $_POST['val_steam'];
        $maxdrivers      = $_POST['series_maxdriv'];
        $maxdriversteam  = $_POST['series_maxdrivteam'];

        $sql  = 'UPDATE Series SET ';

        $values  = array();
        $columns = array();

        echo '<h2>&Aumlnderungen</h2><table>';

        if($name != '')
        {
            $values[]  = $name;
            $columns[] = 'Name';

            echo '<tr><td>' . $_POST['old_name'] . ' -> ' . $name . '<br /></td></tr>';
        }
        if($shortcut != '')
        {
            $values[]  = $shortcut;
            $columns[] = 'Shortcut';

            echo '<tr><td>' . $_POST['old_shortcut'] . ' -> ' . $shortcut . '<br /></td></tr>';

            // Ordner umbennen
            try
            {
                rename($_POST['old_shortcut'], $shortcut);
            }
            catch(Exception $ex)
            {
                echo 'Could not rename folder.<br />';
                echo $ex;
            }
        }
        if($steam_val != '')
        {
            $values[]  = $steam_val;
            $columns[] = 'Steam';

            echo '<tr><td>' . $_POST['old_steam'] . ' -> ' . $steam_val . '<br /></td></tr>';
        }
        if($maxdrivers != '')
        {
            $values[]  = $maxdrivers;
            $columns[] = 'Maximum_drivers';

            echo '<tr><td>' . $_POST['old_maxdriv'] . ' -> ' . $maxdrivers . '<br /></td></tr>';
        }
        if($maxdriversteam != '')
        {
            $values[]  = $maxdriversteam;
            $columns[] = 'Maximum_drivers_per_team';

            echo '<tr><td>' . $_POST['old_maxdrivteam'] . ' -> ' . $maxdriversteam . '<br /></td></tr>';
        }

        for($i = 0; $i < sizeof($values); $i++)
        {
            $sql .= $columns[$i] . ' = \'' . $values[$i] . '\'';
            if(($i + 1) < sizeof($values))
            {
                $sql .= ', ';
            }
        }

        $sql .= ' WHERE ID_Series = ' . $id;

        $db = new Database();
        $db->Connect();
        $db->ExecSQL($sql);
        $db->Disconnect();

        echo '</table><br /><b>Seriendetails wurden ge&aumlndert!</b><br />';
    }
    else
    {
        echo 'ERROR';
    }
?>